﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar.Controls;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;

namespace GUI
{
    public class ExportToPdf
    {
        Document document;
        Table table;
        Table table1;
        Table table2;
        DataGridViewX dgv;
        DataGridViewX dgv1;
        DataGridViewX dgv2;
        public ExportToPdf()
        {
            this.document = new Document();
        }
        public ExportToPdf(DataGridViewX dgview1)
        {
            this.document = new Document();
            this.dgv = dgview1;
        }
        public ExportToPdf(DataGridViewX dgview, DataGridViewX dgview1, DataGridViewX dgview2)
        {
            this.document = new Document();
            this.dgv = dgview;
            this.dgv1 = dgview1;
            this.dgv2 = dgview2;
        }
        #region Thong ke khach hang theo dich vu
        public Document CreateDocumentThongKeKhachhangTheoDichVu(string tongsokhachhangBao, string tongsokhachhangBang, string tongsokhachhangTobuom)
        {
            this.document.Info.Title = "A sample invoice";
            this.document.Info.Subject = "Demonstrates how to create an invoice.";
            this.document.Info.Author = "Stefan Lange";

            DefineStyles();

            CreatePageThongKeKhachHangTheoDichVu(tongsokhachhangBao, tongsokhachhangBang, tongsokhachhangTobuom);

            FillContent3Grid();

            return this.document;
        }
        public void CreatePageThongKeKhachHangTheoDichVu(string bao, string bang, string tobuom)
        {
            // Each MigraDoc document needs at least one section.
            Section section = this.document.AddSection();
            //Create Header
            Paragraph paragraph = section.Headers.Primary.AddParagraph();
            paragraph.AddText("Công Ty Địa Ốc ABC");
            paragraph.Format.Font.Size = 12;
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            // Create footer
            

            paragraph = section.Footers.Primary.AddParagraph();
            paragraph.AddText("TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN");
            paragraph.Format.Font.Size = 9;
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            // Add the print date field
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.5cm";

            paragraph.Style = "Reference";
            paragraph.AddFormattedText("THỐNG KÊ KHÁCH HÀNG THEO DỊCH VỤ", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            //paragraph = section.AddParagraph();
            //paragraph.Format.SpaceBefore = "1cm";
            ////paragraph.AddText("Tháng " + thang + " Năm " + nam);
            //paragraph.AddText("Tổng Số Khách Hàng: " + bao);
            //paragraph.Format.Alignment = ParagraphAlignment.Center;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Dịch Vụ Quảng Cáo Báo");
            paragraph.Format.Alignment = ParagraphAlignment.Left;
            //table1
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";

            // Create the item table
            this.table = section.AddTable();
            this.table.Style = "Table";
            this.table.Borders.Color = tableBorder;
            this.table.Borders.Width = 0.25;
            this.table.Borders.Left.Width = 0.5;
            this.table.Borders.Right.Width = 0.5;
            this.table.Rows.LeftIndent = 0;

            Column column;

            // Before you can add a row, you must define the columns
            column = this.table.AddColumn("1cm");
            column.Format.Alignment = ParagraphAlignment.Center;
            for (int i = 0; i < dgv.Columns.Count - 1; i++)
            {
                float kc = (float)15 / (dgv.Columns.Count - 1);
                column = this.table.AddColumn(kc + "cm");
                column.Format.Alignment = ParagraphAlignment.Right;
            }


            // Create the header of the table

            Row row = table.AddRow();
            row.Height = "0.75cm";
            row.HeadingFormat = true;
            row.Shading.Color = tableBlue;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                row.Cells[i].AddParagraph(dgv.Columns[i].HeaderText);
                row.Cells[i].Format.Alignment = ParagraphAlignment.Left;
                row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
            }

            //////////--Bao--//////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.5cm";
            paragraph.AddText("Tổng số khách hàng : " + bao);
            paragraph.Format.Alignment = ParagraphAlignment.Right;

            //paragraph = section.AddParagraph();
            //paragraph.Format.SpaceBefore = "0.25cm";
            //paragraph.AddText("Tổng số khách hàng : " + bang);
            //paragraph.Format.Alignment = ParagraphAlignment.Right;
            /////////////////////////////////////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Dịch Vụ Quảng Cáo Bảng");
            paragraph.Format.Alignment = ParagraphAlignment.Left;
            //table 2
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";

            // Create the item table
            this.table1 = section.AddTable();
            this.table1.Style = "Table";
            this.table1.Borders.Color = tableBorder;
            this.table1.Borders.Width = 0.25;
            this.table1.Borders.Left.Width = 0.5;
            this.table1.Borders.Right.Width = 0.5;
            this.table1.Rows.LeftIndent = 0;

            Column column1;

            // Before you can add a row, you must define the columns
            column1 = this.table1.AddColumn("1cm");
            column1.Format.Alignment = ParagraphAlignment.Center;
            for (int i = 0; i < dgv1.Columns.Count - 1; i++)
            {
                float kc = (float)15 / (dgv1.Columns.Count - 1);
                column1 = this.table1.AddColumn(kc + "cm");
                column1.Format.Alignment = ParagraphAlignment.Right;
            }


            // Create the header of the table

            Row row1 = table1.AddRow();
            row1.Height = "0.75cm";
            row1.HeadingFormat = true;
            row1.Shading.Color = tableBlue;

            for (int i = 0; i < dgv1.Columns.Count; i++)
            {
                row1.Cells[i].AddParagraph(dgv1.Columns[i].HeaderText);
                row1.Cells[i].Format.Alignment = ParagraphAlignment.Left;
                row1.Cells[i].VerticalAlignment = VerticalAlignment.Center;
            }

            //////////--Bao--//////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.5cm";
            paragraph.AddText("Tổng số khách hàng : " + bang);
            paragraph.Format.Alignment = ParagraphAlignment.Right;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Dịch Vụ Quảng Cáo Tờ Bướm");
            paragraph.Format.Alignment = ParagraphAlignment.Left;
            //table 3
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";

            // Create the item table
            this.table2 = section.AddTable();
            this.table2.Style = "Table";
            this.table2.Borders.Color = tableBorder;
            this.table2.Borders.Width = 0.25;
            this.table2.Borders.Left.Width = 0.5;
            this.table2.Borders.Right.Width = 0.5;
            this.table2.Rows.LeftIndent = 0;

            Column column2;

            // Before you can add a row, you must define the columns
            column2 = this.table2.AddColumn("1cm");
            column2.Format.Alignment = ParagraphAlignment.Center;
            for (int i = 0; i < dgv2.Columns.Count - 1; i++)
            {
                float kc = (float)15 / (dgv2.Columns.Count - 1);
                column2 = this.table2.AddColumn(kc + "cm");
                column2.Format.Alignment = ParagraphAlignment.Right;
            }


            // Create the header of the table

            Row row2 = table2.AddRow();
            row2.Height = "0.75cm";
            row2.HeadingFormat = true;
            row2.Shading.Color = tableBlue;

            for (int i = 0; i < dgv2.Columns.Count; i++)
            {
                row2.Cells[i].AddParagraph(dgv2.Columns[i].HeaderText);
                row2.Cells[i].Format.Alignment = ParagraphAlignment.Left;
                row2.Cells[i].VerticalAlignment = VerticalAlignment.Center;
            }

            //////////--To buom--//////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.5cm";
            paragraph.AddText("Tổng số khách hàng : " + tobuom);
            paragraph.Format.Alignment = ParagraphAlignment.Right;


            paragraph = section.AddParagraph("");
            paragraph.Format.SpaceBefore = "5cm";
            paragraph.Style = "Reference";
            paragraph.AddFormattedText("", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            paragraph.AddText("Tp Hồ Chí Minh , ");
            paragraph.AddDateField("dd/MM/yyyy");


            paragraph = section.AddParagraph();
            paragraph.Format.SpaceAfter = "0.5cm";
            //paragraph.Format.Borders.DistanceFromBottom = "2cm";
            paragraph.AddText("Người Lập phiếu ");
            paragraph.Format.Alignment = ParagraphAlignment.Right;

        }
        #endregion
        #region Phieu THU
        public Document CreateDocumentPhieuThu1(string hoten, string diachi, string sodienthoai, string email, string diachidiaoc, string tendichvu, string sotien, string tungay, string denngay,string maphieudangky)
        {
            this.document.Info.Title = "A sample invoice";
            this.document.Info.Subject = "Demonstrates how to create an invoice.";
            this.document.Info.Author = "Stefan Lange";

            DefineStyles();

            CreatePagePhieuThu1(hoten, diachi, sodienthoai,email,diachidiaoc,tendichvu,sotien,tungay,denngay,maphieudangky);

            //FillContent();

            return this.document;
        }
        public void CreatePagePhieuThu1(string hoten, string diachi, string sodienthoai, string email, string diachidiaoc, string tendichvu, string sotien, string tungay, string denngay, string maphieudangky)
        {
            // Each MigraDoc document needs at least one section.
            Section section = this.document.AddSection();
            //Create Header
            Paragraph paragraph = section.Headers.Primary.AddParagraph();
            paragraph.AddText("Công Ty Địa Ốc ABC");
            paragraph.Format.Font.Size = 12;
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            // Create footer
            paragraph = section.Footers.Primary.AddParagraph();
            paragraph.AddText("Trường đại học công nghệ thông tin");
            paragraph.Format.Font.Size = 9;
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            // Add the print date field
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1.5cm";

            paragraph.Style = "Reference";
            paragraph.AddFormattedText("Phiếu Thu Tiền", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            ///////////////////////////////////////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Mã Phiếu Đăng Ký : " + maphieudangky);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Họ Tên : " + hoten);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Số Điện Thoại : " + sodienthoai);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Email : " + email);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Địa Chỉ : " + diachi);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Địa Chỉ Địa Ốc : " + diachidiaoc);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Từ Ngày : " + tungay);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Đến Ngày : " + denngay);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Tên Dịch Vụ : " + tendichvu);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            // paragraph.AddTab();
            //paragraph.AddSpace(50);
            //paragraph.AddText("Dịch Vụ : " + tendichvu);

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";


            /////////////////////////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.5cm";
            paragraph.AddText("Tổng Tiền : " + sotien);
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            /////////////////////////////////////////////

            paragraph = section.AddParagraph("");
            paragraph.Format.SpaceBefore = "5cm";
            paragraph.Style = "Reference";
            paragraph.AddFormattedText("", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            paragraph.AddText("Tp Hồ Chí Minh , ");
            paragraph.AddDateField("dd/MM/yyyy");


            paragraph = section.AddParagraph();
            paragraph.Format.SpaceAfter = "0.5cm";
            //paragraph.Format.Borders.DistanceFromBottom = "2cm";
            paragraph.AddText("Người Lập phiếu ");
            paragraph.Format.Alignment = ParagraphAlignment.Right;



        }
        #endregion
        #region 2
        //2
        public Document CreateDocumentPhieuThu2(string hoten, string diachi, string sodienthoai, string email, string diachidiaoc, string tendichvu1, string tendichvu2, string sotien, string tungay, string denngay, string maphieudangky)
        {
            this.document.Info.Title = "A sample invoice";
            this.document.Info.Subject = "Demonstrates how to create an invoice.";
            this.document.Info.Author = "Stefan Lange";

            DefineStyles();

            CreatePagePhieuThu2(hoten, diachi, sodienthoai, email, diachidiaoc, tendichvu1,tendichvu2, sotien, tungay, denngay,maphieudangky);

            //FillContent();

            return this.document;
        }
        public void CreatePagePhieuThu2(string hoten, string diachi, string sodienthoai, string email, string diachidiaoc, string tendichvu1, string tendichvu2, string sotien, string tungay, string denngay, string maphieudangky)
        {
            // Each MigraDoc document needs at least one section.
            Section section = this.document.AddSection();
            //Create Header
            Paragraph paragraph = section.Headers.Primary.AddParagraph();
            paragraph.AddText("Công Ty Địa Ốc ABC");
            paragraph.Format.Font.Size = 12;
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            // Create footer
            paragraph = section.Footers.Primary.AddParagraph();
            paragraph.AddText("Trường đại học công nghệ thông tin");
            paragraph.Format.Font.Size = 9;
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            // Add the print date field
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1.5cm";

            paragraph.Style = "Reference";
            paragraph.AddFormattedText("PHIẾU THU TIỀN", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            ///////////////////////////////////////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Mã Phiếu Đăng Ký : " + maphieudangky);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Họ Tên : " + hoten);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Số Điện Thoại : " + sodienthoai);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Email : " + email);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Địa Chỉ : " + diachi);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Địa Chỉ Địa Ốc : " + diachidiaoc);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Từ Ngày : " + tungay);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Đến Ngày : " + denngay);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Tên Dịch Vụ : " + tendichvu1);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Tên Dịch Vụ : " + tendichvu2);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            // paragraph.AddTab();
            //paragraph.AddSpace(50);
            //paragraph.AddText("Dịch Vụ : " + tendichvu);

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";


            /////////////////////////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.5cm";
            paragraph.AddText("Tổng Tiền : " + sotien);
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            /////////////////////////////////////////////

            paragraph = section.AddParagraph("");
            paragraph.Format.SpaceBefore = "5cm";
            paragraph.Style = "Reference";
            paragraph.AddFormattedText("", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            paragraph.AddText("Tp Hồ Chí Minh , ");
            paragraph.AddDateField("dd/MM/yyyy");


            paragraph = section.AddParagraph();
            paragraph.Format.SpaceAfter = "0.5cm";
            //paragraph.Format.Borders.DistanceFromBottom = "2cm";
            paragraph.AddText("Người Lập phiếu ");
            paragraph.Format.Alignment = ParagraphAlignment.Right;
        }
        //3
        #endregion
        #region 3
        public Document CreateDocumentPhieuThu3(string hoten, string diachi, string sodienthoai, string email, string diachidiaoc, string tendichvu1, string tendichvu2, string tendichvu3, string sotien, string tungay, string denngay,string maphieudangky)
        {
            this.document.Info.Title = "A sample invoice";
            this.document.Info.Subject = "Demonstrates how to create an invoice.";
            this.document.Info.Author = "Stefan Lange";

            DefineStyles();

            CreatePagePhieuThu3(hoten, diachi, sodienthoai, email, diachidiaoc, tendichvu1, tendichvu2, tendichvu3, sotien, tungay, denngay,maphieudangky);

            //FillContent();

            return this.document;
        }
        public void CreatePagePhieuThu3(string hoten, string diachi, string sodienthoai, string email, string diachidiaoc, string tendichvu1, string tendichvu2, string tendichvu3, string sotien, string tungay, string denngay,string maphieudangky)
        {
            // Each MigraDoc document needs at least one section.
            Section section = this.document.AddSection();
            //Create Header
            Paragraph paragraph = section.Headers.Primary.AddParagraph();
            paragraph.AddText("Công Ty Địa Ốc ABC");
            paragraph.Format.Font.Size = 12;
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            // Create footer
            paragraph = section.Footers.Primary.AddParagraph();
            paragraph.AddText("Trường đại học công nghệ thông tin");
            paragraph.Format.Font.Size = 9;
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            // Add the print date field
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1.5cm";

            paragraph.Style = "Reference";
            paragraph.AddFormattedText("LẬP PHIẾU THU TIỀN", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            ///////////////////////////////////////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Mã Phiếu Đăng Ký : " + maphieudangky);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Họ Tên : " + hoten);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Số Điện Thoại : " + sodienthoai);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Email : " + email);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Địa Chỉ : " + diachi);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Địa Chỉ Địa Ốc : " + diachidiaoc);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Từ Ngày : " + tungay);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Đến Ngày : " + denngay);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Tên Dịch Vụ : " + tendichvu1);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Tên Dịch Vụ : " + tendichvu2);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Tên Dịch Vụ : " + tendichvu3);
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            // paragraph.AddTab();
            //paragraph.AddSpace(50);
            //paragraph.AddText("Dịch Vụ : " + tendichvu);

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";


            /////////////////////////////////
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.5cm";
            paragraph.AddText("Tổng Tiền : " + sotien);
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            /////////////////////////////////////////////

            paragraph = section.AddParagraph("");
            paragraph.Format.SpaceBefore = "5cm";
            paragraph.Style = "Reference";
            paragraph.AddFormattedText("", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            paragraph.AddText("Tp Hồ Chí Minh , ");
            paragraph.AddDateField("dd/MM/yyyy");


            paragraph = section.AddParagraph();
            paragraph.Format.SpaceAfter = "0.5cm";
            //paragraph.Format.Borders.DistanceFromBottom = "2cm";
            paragraph.AddText("Người Lập phiếu ");
            paragraph.Format.Alignment = ParagraphAlignment.Right;



        }


        #endregion
        #region bao cao doanh thu
        public Document CreateDocumentBaoCaoDoanhThu(string thang, string nam,  string tongsodoanhthu)
        {
            this.document.Info.Title = "A sample invoice";
            this.document.Info.Subject = "Demonstrates how to create an invoice.";
            this.document.Info.Author = "Stefan Lange";

            DefineStyles();

            CreatePageBaoCaoDoanhThu(thang, nam, tongsodoanhthu);

            FillContent();

            return this.document;
        }

        

        //public Document CreateDocumentHoaDon(string hoten, string ngaykham, string tienkham, string tienthuoc, string tongtien)
        //{
        //    this.document.Info.Title = "A sample invoice";
        //    this.document.Info.Subject = "Demonstrates how to create an invoice.";
        //    this.document.Info.Author = "Stefan Lange";

        //    DefineStyles();

        //    CreatePageHoDon(hoten, ngaykham, tienkham, tienthuoc, tongtien);

        //    FillContent();

        //    return this.document;
        //}
       

        public void CreatePageBaoCaoDoanhThu(string thang, string nam, string tongsodoanhthu)
        {
            // Each MigraDoc document needs at least one section.
            Section section = this.document.AddSection();
            //Create Header
            Paragraph paragraph = section.Headers.Primary.AddParagraph();
            paragraph.AddText("Công Ty Địa Ốc ABC");
            paragraph.Format.Font.Size = 12;
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            //// Create footer
            //paragraph = section.Footers.Primary.AddParagraph();
            //paragraph.AddText("Trường đại học công nghệ thông tin");
            //paragraph.Format.Font.Size = 9;
            //paragraph.Format.Alignment = ParagraphAlignment.Center;

            // Add the print date field
            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";

            paragraph.Style = "Reference";
            paragraph.AddFormattedText("BÁO CÁO DOANH THU", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "1cm";
            paragraph.AddText("Tháng " + thang + " Năm " + nam);
            paragraph.Format.Alignment = ParagraphAlignment.Center;


            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";

            // Create the item table
            this.table = section.AddTable();
            this.table.Style = "Table";
            this.table.Borders.Color = tableBorder;
            this.table.Borders.Width = 0.25;
            this.table.Borders.Left.Width = 0.5;
            this.table.Borders.Right.Width = 0.5;
            this.table.Rows.LeftIndent = 0;

            Column column;

            // Before you can add a row, you must define the columns
            column = this.table.AddColumn("1cm");
            column.Format.Alignment = ParagraphAlignment.Center;
            for (int i = 0; i < dgv.Columns.Count - 1; i++)
            {
                float kc = (float)15 / (dgv.Columns.Count - 1);
                column = this.table.AddColumn(kc + "cm");
                column.Format.Alignment = ParagraphAlignment.Right;
            }


            // Create the header of the table

            Row row = table.AddRow();
            row.Height = "0.75cm";
            row.HeadingFormat = true;
            row.Shading.Color = tableBlue;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                row.Cells[i].AddParagraph(dgv.Columns[i].HeaderText);
                row.Cells[i].Format.Alignment = ParagraphAlignment.Left;
                row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
            }

            /////////////////////////////////
            

            paragraph = section.AddParagraph();
            paragraph.Format.SpaceBefore = "0.25cm";
            paragraph.AddText("Tổng số doanh thu : " + tongsodoanhthu);
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            /////////////////////////////////////////////


            paragraph = section.AddParagraph("");
            paragraph.Format.SpaceBefore = "2cm";
            paragraph.Style = "Reference";
            paragraph.AddFormattedText("", TextFormat.Bold);
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            paragraph.AddText("Tp Hồ Chí Minh , ");
            paragraph.AddDateField("dd/MM/yyyy");


            paragraph = section.AddParagraph();
            paragraph.Format.SpaceAfter = "0.5cm";
            //paragraph.Format.Borders.DistanceFromBottom = "2cm";
            paragraph.AddText("Người Lập phiếu ");
            paragraph.Format.Alignment = ParagraphAlignment.Right;
        }
        #endregion
        public void DefineStyles()
        {
            Style style;
            // Get the predefined style Normal.
            style = this.document.Styles["Normal"];
            style.Font.Name = "Verdana";

            style = this.document.Styles[StyleNames.Header];
            style.ParagraphFormat.AddTabStop("16cm", MigraDoc.DocumentObjectModel.TabAlignment.Right);

            style = this.document.Styles[StyleNames.Footer];
            style.ParagraphFormat.AddTabStop("8cm", MigraDoc.DocumentObjectModel.TabAlignment.Center);

            // Create a new style called Table based on style Normal
            style = this.document.Styles.AddStyle("Table", "Normal");
            style.Font.Name = "Verdana";
            style.Font.Size = 9;

            // Create a new style called Reference based on style Normal
            style = this.document.Styles.AddStyle("Reference", "Normal");
            style.ParagraphFormat.SpaceBefore = "5mm";
            style.ParagraphFormat.SpaceAfter = "5mm";
            style.ParagraphFormat.TabStops.AddTabStop("16cm", MigraDoc.DocumentObjectModel.TabAlignment.Right);

        }

       

        public void FillContent()
        {
            for (int i = 0; i < dgv.Rows.Count - 1; i++)
            {
                Row row = table.AddRow();
                row.Height = "0.75cm";
                row.HeadingFormat = true;
                row.Shading.Color = tableGray;
                for (int j = 0; j < dgv.Columns.Count; j++)
                {
                    try
                    {
                        if (dgv[j, i].Value != null)
                        {
                            row.Cells[j].AddParagraph(dgv[j, i].Value.ToString());
                            row.Cells[j].Format.Alignment = ParagraphAlignment.Left;
                            row.Cells[j].VerticalAlignment = VerticalAlignment.Center;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }


                }
            }
        }
        public void FillContent3Grid()
        {
            for (int i = 0; i < dgv.Rows.Count - 1; i++)
            {
                Row row = table.AddRow();
                row.Height = "0.75cm";
                row.HeadingFormat = true;
                row.Shading.Color = tableGray;
                for (int j = 0; j < dgv.Columns.Count; j++)
                {
                    try
                    {
                        if (dgv[j, i].Value != null)
                        {
                            row.Cells[j].AddParagraph(dgv[j, i].Value.ToString());
                            row.Cells[j].Format.Alignment = ParagraphAlignment.Left;
                            row.Cells[j].VerticalAlignment = VerticalAlignment.Center;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }


                }
            }
            //
            for (int i = 0; i < dgv1.Rows.Count - 1; i++)
            {
                Row row = table1.AddRow();
                row.Height = "0.75cm";
                row.HeadingFormat = true;
                row.Shading.Color = tableGray;
                for (int j = 0; j < dgv1.Columns.Count; j++)
                {
                    try
                    {
                        if (dgv1[j, i].Value != null)
                        {
                            row.Cells[j].AddParagraph(dgv1[j, i].Value.ToString());
                            row.Cells[j].Format.Alignment = ParagraphAlignment.Left;
                            row.Cells[j].VerticalAlignment = VerticalAlignment.Center;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }


                }
            }
            //
            for (int i = 0; i < dgv2.Rows.Count - 1; i++)
            {
                Row row = table2.AddRow();
                row.Height = "0.75cm";
                row.HeadingFormat = true;
                row.Shading.Color = tableGray;
                for (int j = 0; j < dgv2.Columns.Count; j++)
                {
                    try
                    {
                        if (dgv2[j, i].Value != null)
                        {
                            row.Cells[j].AddParagraph(dgv2[j, i].Value.ToString());
                            row.Cells[j].Format.Alignment = ParagraphAlignment.Left;
                            row.Cells[j].VerticalAlignment = VerticalAlignment.Center;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }


                }
            }
        }
        
        public void SaveFilePdf(Document document, string file)
        {
            //Document document = CreateDocument();
            // for debugging only...
            MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, "MigraDoc.mdddl");

            // Create a renderer for PDF that uses Unicode font encoding
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);

            // Set the MigraDoc document
            pdfRenderer.Document = document;

            // Create the PDF document
            pdfRenderer.RenderDocument();

            // I don't want to close the document constantly...
            //filename = "Invoice-" + Guid.NewGuid().ToString("N").ToUpper() + ".pdf";
            pdfRenderer.Save(file);
            // ...and start a viewer.
            // Process.Start(filename);
        }

        static MigraDoc.DocumentObjectModel.Color tableBorder = new MigraDoc.DocumentObjectModel.Color(81, 125, 192);
        static MigraDoc.DocumentObjectModel.Color tableBlue = new MigraDoc.DocumentObjectModel.Color(235, 240, 249);
        static MigraDoc.DocumentObjectModel.Color tableGray = new MigraDoc.DocumentObjectModel.Color(242, 242, 242);
    }
}
